<template>
  <div class="chart-item row1-medium">
    <div class="chart-header">
      <h3>物料库房分布</h3>
      <div class="chart-tabs">
        <el-radio-group v-model="distributionType" @change="handleDistributionTypeChange">
          <el-radio-button label="warehouse">库房分布</el-radio-button>
          <el-radio-button label="material">物料品种</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="chart-content">
      <div ref="warehouseDistributionChart" class="chart"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'WarehouseDistributionChart',
  data() {
    return {
      distributionType: 'warehouse',
      chart: null
    }
  },
  mounted() {
    // 延迟初始化，确保DOM完全渲染
    setTimeout(() => {
      this.initChart()
    }, 100)
  },
  beforeUnmount() {
    if (this.chart) {
      this.chart.dispose()
    }
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        const chartDom = this.$refs.warehouseDistributionChart
        if (!chartDom) return

        this.chart = echarts.init(chartDom)
        this.updateChart()

        // 监听窗口大小变化
        window.addEventListener('resize', () => {
          if (this.chart) {
            this.chart.resize()
          }
        })
      })
    },

    updateChart() {
      if (!this.chart) return
      const warehouseData = [
        { name: '华东库房', value: 2845, color: '#5470c6' },
        { name: '华南库房', value: 2156, color: '#91cc75' },
        { name: '华北库房', value: 1876, color: '#fac858' },
        { name: '西南库房', value: 1543, color: '#ee6666' },
        { name: '其他库房', value: 892, color: '#73c0de' }
      ]

      const materialData = [
        { name: '原材料', value: 3245, color: '#5470c6' },
        { name: '半成品', value: 2876, color: '#91cc75' },
        { name: '成品', value: 2156, color: '#fac858' },
        { name: '辅料', value: 1234, color: '#ee6666' },
        { name: '包装材料', value: 801, color: '#73c0de' }
      ]

      const currentData = this.distributionType === 'warehouse' ? warehouseData : materialData

      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          show: false
        },
        series: [
          {
            name: this.distributionType === 'warehouse' ? '库房分布' : '物料品种',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '50%'],
            data: currentData.map(item => ({
              value: item.value,
              name: item.name,
              itemStyle: { color: item.color }
            })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              show: true,
              formatter: '{b}\n{c}'
            }
          }
        ]
      }

      this.chart.setOption(option, true)
    },

    handleDistributionTypeChange(type) {
      this.distributionType = type
      this.updateChart()
      this.$emit('distribution-type-change', type)
    }
  }
}
</script>

<style lang="scss" scoped>
/* 组件特有样式 */
.chart-tabs {
  ::v-deep .el-radio-button__inner {
    padding: 6px 12px;
    font-size: 12px;
  }
}

.chart-content {
  .chart {
    width: 100%;
    height: 300px;
  }
}
</style>
